/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.isd.promocion.dao;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import com.isd.global.dao.BaseDAO;
import com.isd.promocion.modelo.Promocion;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Claudio
 */
@Stateless
@LocalBean
public class PromocionDAO extends BaseDAO{
    
    public List<Promocion> getPromocionesActivas(){

        try {

            String sQuery = "SELECT p FROM Promocion p  WHERE ";
            sQuery += " p.disponibleDesde <= :desde ";
            sQuery += " AND p.disponibleHasta >= :hasta ";
            sQuery += " AND  p.debaja = 'N' ";
            sQuery += " ORDER BY p.nroPromocion ";

            Query q = getEntityManager().createQuery(sQuery);

            q.setParameter("desde", new java.util.Date());
            q.setParameter("hasta", new java.util.Date());


            //Por seguridad seteamos en 10
            q.setMaxResults(10);

            return q.getResultList();

        } catch (Exception e) {

            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getPromocionesActivas", e);            
            return new ArrayList<Promocion>();
        }
    }

    public Promocion getPromocion(Integer id) {

        try {

            String sQuery = "SELECT p FROM Promocion p  WHERE ";
            sQuery += " p.nroPromocion = :nroPromocion ";
            sQuery += " AND p.disponibleDesde <= :desde ";
            sQuery += " AND p.disponibleHasta >= :hasta ";
            sQuery += " AND  p.debaja = 'N' ";
            sQuery += " ORDER BY p.nroPromocion ";

            Query q = getEntityManager().createQuery(sQuery);

            q.setParameter("nroPromocion", id);
            q.setParameter("desde", new java.util.Date());
            q.setParameter("hasta", new java.util.Date());

            return (Promocion) q.getSingleResult();

        } catch(NoResultException nre){
            
            return null;

        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getPromocion", e);                                
            return null;
        }
    }

    public List<Promocion> getPromocionByNombre(String query) {
        
        try {

            String sQuery = "SELECT p FROM Promocion p  WHERE ";
            sQuery += " p.disponibleDesde <= :desde ";
            sQuery += " AND p.disponibleHasta >= :hasta ";
            sQuery += " AND  p.debaja = 'N' ";
            sQuery += " AND  p.descripcion like :sQuery ";
            sQuery += " ORDER BY p.nroPromocion ";

            Query q = getEntityManager().createQuery(sQuery);

            q.setParameter("desde", new java.util.Date());
            q.setParameter("hasta", new java.util.Date());
            q.setParameter("sQuery", "%"+query+"%");
            

            //Por seguridad seteamos en 10
            q.setMaxResults(10);

            return q.getResultList();

        } catch (Exception e) {

            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getPromocionByNombre", e);            
            return new ArrayList<Promocion>();
        }
    }
}
